package ycl.scheduled.common.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import ycl.scheduled.common.base.entity.BaseRecordEntity;
import ycl.scheduled.common.base.entity.BaseSelectOneEntity;
import ycl.scheduled.common.base.mapper.BaseRecordMapper;
import ycl.scheduled.common.base.mapper.BaseSelectOneMapper;
import ycl.scheduled.common.base.service.BaseSelectOneService;
import ycl.scheduled.common.utils.BeanConvertUtil;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 只查看一条数据
 *
 * @author: YCL
 * @date: 2021-08-23 20:40:13
 */
@Service
@Lazy
public abstract class
BaseSelectOneServiceImpl<M extends BaseSelectOneMapper<T>, T extends BaseSelectOneEntity>
		extends BaseServiceImpl<M, T>
		implements BaseSelectOneService<T> {

	@Resource
	private BaseSelectOneMapper<T> mapper;

	@Override
	public T selectOne() {
		LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
		wrapper.last("limit 1");
		return mapper.selectOne(wrapper);
	}


	/**
	 * 重新修改数据并记录之前数据(半成品)
	 * <p>
	 * 后续解决: 方法参数能不能用 Class<E> 来替代 E, 只要传入对应的类即可, 无需传入具体的变量
	 *
	 * @param t   当前要修改的实体类
	 * @param e   对应的记录类
	 * @param r   记录类对应的数据库层
	 * @param <E> 记录类
	 * @param <R> 记录类把数据库层
	 */
	public <E extends BaseRecordEntity, R extends BaseRecordMapper<E>> void resave(T t, E e, R r, Long userId) {
		mapper.delete(null);
		mapper.insert(t);

		BeanConvertUtil.convert(t, e, "id");
		e.setUpdateTime(LocalDateTime.now())
				.setUpdateUser(userId);
		r.insert(e);
	}


}
